//
//  Extension+NSDate.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/27.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
private let df = NSDateFormatter()

private let calendar: NSCalendar = NSCalendar.currentCalendar()
extension NSDate {
    
    class func sinaDate(creatAt: String) -> NSDate? {
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        
        let sinaDateStr = df.dateFromString(creatAt)
        
        return sinaDateStr
    }
    // 获取微博时间字符串
    var sinaDescripitionStr: String{
        
        let isThisYear = sinaDateIsThisYear(self)
        
        if isThisYear{
            
            if calendar.isDateInToday(self){
                
                let s = Int(NSDate().timeIntervalSinceDate(self))
                
                if s < 60{
                    
                    return "刚刚"
                }else if s >= 60 && s < 3600{
                    return "\(s/60)分钟前"
                }else if s >= 3600{
                    
                    return "\(s/3600)小时前"
                }
            }else if calendar.isDateInYesterday(self){
                
                df.dateFormat = "昨天 HH:mm"
            }else{
                df.dateFormat = "MM月dd日 HH:mm"
            }
        }else{
            
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
        }
        
        //返回值是String
        return df.stringFromDate(self)
        
    }
    
    
    
    func sinaDateIsThisYear(sinaDate: NSDate)->Bool{
        
        //指定时区
        df.locale = NSLocale(localeIdentifier: "en_US")
        //指定时间格式
        df.dateFormat = "yyyy"
        //获取当前时间
        let currentTimer = NSDate()
        //获取当前日期字符串
        let currentTimerStr = df.stringFromDate(currentTimer)
        //获取微博字符串获取时间
        let sinaDateStr = df.stringFromDate(sinaDate)
        //返回
        
        return currentTimerStr == sinaDateStr
    }
}
